package com.machine.bootstatemachine.service.ordermachine;

import com.machine.bootstatemachine.dao.OrderMapper;
import com.machine.bootstatemachine.domain.Order;
import com.machine.bootstatemachine.enums.OrderEventEnum;
import com.machine.bootstatemachine.enums.OrderStatueEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.statemachine.action.Action;
import org.springframework.stereotype.Component;

/**
 * @Author dai
 * @Date 2021/12/27
 */
@Slf4j
@Component
public class OrderStatusAction {

    @Autowired
    private OrderMapper orderMapper;

    public Action<OrderStatueEnum, OrderEventEnum> payOrder() {
        return context -> {
            log.info("订单支付动作触发状态机状态流转变更, {}", context);
            // 参数
            Order order = (Order) context.getMessageHeader("order");

            // 目标状态
            Integer status = context.getTarget().getId().getStatus();

            order.setStatus(status);
            // 业务状态变更
            orderMapper.updateById(order);

            //todo 业务流水记录

            // todo 操作日志记录

        };
    }

    public Action<OrderStatueEnum, OrderEventEnum> payOrderError() {
        return context -> {
            log.info("触发失败，context : {}", context);
        };
    }

    public Action<OrderStatueEnum, OrderEventEnum> orderDone() {
        return context -> {
            log.info("订单完成动作触发状态机状态流转变更, {}", context);
            // 参数
            Order order = (Order) context.getMessageHeader("order");

            // 目标状态
            Integer status = context.getTarget().getId().getStatus();

            order.setStatus(status);
            // 业务状态变更
            orderMapper.updateById(order);

            //todo 业务流水记录

            // todo 操作日志记录
        };
    }

    public Action<OrderStatueEnum, OrderEventEnum> orderDoneError() {
        return context -> {
            log.info("触发失败，context : {}", context);
        };
    }
}
